/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;

using EnvDTE;

using DslEditorPowerToy.VisualStudio;

namespace DslEditorPowerToy.Recipes.Converters
{
    public class ProjectItemExistsConverter : StringConverterBase
    {
        private string _projectArgumentName = string.Empty;
        private DteHelper.ProjectItemType _itemType = DteHelper.ProjectItemType.File;

        #region Public Methods
        public override void Configure(StringDictionary attributes)
        {
            if (false == attributes.ContainsKey("ProjectArgument"))
                throw new ArgumentNullException("ProjectArgument");
            this._projectArgumentName = (string)attributes["ProjectArgument"];
            if (true == attributes.ContainsKey("ItemType"))
                this._itemType = (DteHelper.ProjectItemType)Enum.Parse(typeof(DteHelper.ProjectItemType), attributes["ItemType"], true);
        }

        public override bool IsValid(ITypeDescriptorContext context, object value)
        {
            if (value is string)
            {
                //Dereference project argument
                Project project = (Project)this.GetArgumentValue(context,this._projectArgumentName);
                if (null == project)
                    return false;

                //Get file from project
                ProjectItem fileItem = DteHelper.FindProjectItemByName(project, (string)value, this._itemType);
                return (null != fileItem);
            }

            return base.IsValid(context, value);
        }
        #endregion
    }
}
